package com.yanqu.road.dao.impl.activity.peakseacraft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftCrossUserGuessData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 */
public class PeakSeacraftCrossUserGuessDataDaoImpl extends TempDataObjectDao<PeakSeacraftCrossUserGuessData> {
    public static String getPeakSeacraftCrossUserGuessDataSql = "select * from `t_c_activity_peak_seacraft_guess` where `activityId`=? and unionGroup=?";

    public Map<Long, PeakSeacraftCrossUserGuessData> getPeakSeacraftCrossUserGuessDataList(int activityId, int unionGroup) {
        Map<Long, PeakSeacraftCrossUserGuessData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getPeakSeacraftCrossUserGuessDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, unionGroup);
            rs = call.executeQuery();
            while (rs.next()) {
                PeakSeacraftCrossUserGuessData data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    public static String updatePeakSeacraftCrossUserGuessDataSql = "update `t_c_activity_peak_seacraft_guess` set serverId=?,guessMap=? where activityId=? and unionGroup=? and userId=?";
    @Override
    protected boolean updateData(PeakSeacraftCrossUserGuessData peakSeacraftCrossUserGuessData) {
        peakSeacraftCrossUserGuessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUserGuessData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUserGuessData.getGuessMap())));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserGuessData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserGuessData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUserGuessData.getUserId()));
        if (getDb().executeNoneQuery(updatePeakSeacraftCrossUserGuessDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertPeakSeacraftCrossUserGuessDataSql = "insert into `t_c_activity_peak_seacraft_guess`(activityId,unionGroup,userId,serverId,guessMap) values (?,?,?,?,?)";
    @Override
    protected boolean addData(PeakSeacraftCrossUserGuessData peakSeacraftCrossUserGuessData) {
        peakSeacraftCrossUserGuessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserGuessData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, peakSeacraftCrossUserGuessData.getUnionGroup()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUserGuessData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, peakSeacraftCrossUserGuessData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(peakSeacraftCrossUserGuessData.getGuessMap())));
        if (getDb().executeNoneQuery(insertPeakSeacraftCrossUserGuessDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected PeakSeacraftCrossUserGuessData parseData(ResultSet rs) throws SQLException {
        PeakSeacraftCrossUserGuessData peakSeacraftCrossUserGuessData = new PeakSeacraftCrossUserGuessData();
        peakSeacraftCrossUserGuessData.setActivityId(rs.getInt("activityId"));
        peakSeacraftCrossUserGuessData.setUnionGroup(rs.getInt("unionGroup"));
        peakSeacraftCrossUserGuessData.setUserId(rs.getLong("userId"));
        peakSeacraftCrossUserGuessData.setServerId(rs.getLong("serverId"));
        peakSeacraftCrossUserGuessData.setGuessMap(JSON.parseObject(rs.getString("guessMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, com.yanqu.road.entity.activity.peakseacraft.PeakSeacraftGuessInfo>>() {}));

        return peakSeacraftCrossUserGuessData;
    }

}